package com.chen.music.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chen.music.model.Collect;
import com.chen.music.service.CollectService;
import com.chen.music.util.CommonResult;
import com.chen.music.util.MessageConst;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 收藏 前端控制器
 * </p>
 *
 * @author boat
 * @since 2022-01-13
 */
@RestController
@RequestMapping("/collect")
@Api(tags = "收藏管理")
public class CollectController {

    @Autowired
    private CollectService collectService;

    @GetMapping("/{id}")
    @ApiOperation("根据用户id获取收藏详情")
    public CommonResult findById(@PathVariable("id") Integer id) {
        try {
            LambdaQueryWrapper<Collect> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(id != null, Collect::getUserId, id);
            return new CommonResult(200, true, collectService.list(wrapper));
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(500, false, MessageConst.INTERFACE_EXCEPTION);
        }
    }

    @PostMapping("/add")
    @ApiOperation("添加收藏")
    public CommonResult findById(Collect collect) {
        try {
            LambdaQueryWrapper<Collect> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(collect.getUserId() != null, Collect::getUserId, collect.getUserId());
            wrapper.eq(collect.getType() != null, Collect::getType, collect.getType());
            wrapper.eq(collect.getSongId() != null, Collect::getSongId, collect.getSongId());
            Collect realCollect = collectService.getOne(wrapper);
            if (realCollect != null) {
                //说明用户已收藏过该歌曲
                return new CommonResult(200, false, MessageConst.ADD_COLLECT_WARN);
            }
            if (collectService.save(collect)) {
                return new CommonResult(200, true, MessageConst.ADD_COLLECT_SUCCESS);
            }
            return new CommonResult(200, false, MessageConst.ADD_COLLECT_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(500, false, MessageConst.INTERFACE_EXCEPTION);
        }
    }

    @DeleteMapping("delete/{userId}/{songId}")
    @ApiOperation("删除收藏")
    public CommonResult deleteById(@PathVariable("userId") Integer userId, @PathVariable("songId") Integer songId) {
        try {
            LambdaQueryWrapper<Collect> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(userId != null, Collect::getUserId, userId);
            wrapper.eq(songId != null, Collect::getSongId, songId);
            Collect collect = collectService.getOne(wrapper);
            if (collect != null) {
                if (collectService.removeById(collect.getId())) {
                    return new CommonResult(200, true, MessageConst.DELETE_COLLECT_SUCCESS);
                }
                return new CommonResult(200, false, MessageConst.DELETE_COLLECT_FAIL);
            }
            return new CommonResult(200, false, MessageConst.DELETE_COLLECT_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(500, false, MessageConst.INTERFACE_EXCEPTION);
        }
    }
}

